package com.definesys.smecrailtransit.module.MonthForm.dialog;

import android.app.Dialog;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;

import com.definesys.smecrailtransit.R;
import com.definesys.smecrailtransit.api.SmecRailApi;
import com.definesys.smecrailtransit.api.SmecRailConstant;
import com.definesys.smecrailtransit.databinding.DialogMonthFormBinding;
import com.definesys.smecrailtransit.module.MonthForm.MonthFormPresenter;
import com.definesys.smecrailtransit.module.MonthForm.PopWindowAdapter.PopCarePlanAdapter;
import com.definesys.smecrailtransit.module.MonthForm.PopWindowAdapter.PopFaultLogAdapter;
import com.definesys.smecrailtransit.module.MonthForm.PopWindowAdapter.PopMonthAdapter;
import com.definesys.smecrailtransit.module.MonthForm.PopWindowAdapter.PopWindowAdapter;
import com.definesys.smecrailtransit.module.MonthForm.bean.CarePlanBean;
import com.definesys.smecrailtransit.module.MonthForm.bean.FaultLogBean;
import com.definesys.smecrailtransit.module.MonthForm.bean.PositionBean;
import com.definesys.smecrailtransit.utils.CommonUtils;
import com.hwangjr.rxbus.Bus;
import com.hwangjr.rxbus.SmecRxBus;
import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;


/**
 * Created by cjy on 2017/8/31.
 */

public class MonthFormDialog extends Dialog {
    public static final String GET_STATION_FORM_SUCCESS = "getStationFormSuccess(Object o)";
    public static final String GET_STATION_FORM_FAILED = "getStationFormFailed(Object o)";
    public static final String GET_FAULT_LOG_SUCCESS = "getFaultLogSuccess(Object o)";
    public static final String GET_CARE_PLAN_SUCCESS = "getCarePlanSuccess(Object o)";
    public static final String GET_FAULT_LOG_FAILED = "getFaultLogFailed(Object o)";
    public static final String GET_CARE_PLAN_FAILED = "getCarePlanFailed(Object o)";
    public static final String SHOW_POP_DETAIL = "showPopDetail(Object o)";
    public static final String CHANGE_POP_MONTH = "changePopMonth(Object o)";

    private Context mContext;
    protected Bus mRxBus;
    private DisplayMetrics dm;
    private DialogMonthFormBinding dialogMonthFormBinding;
    private MonthFormPresenter monthFormPresenter;
    private PopWindowAdapter popWindowAdapter;
    private PopFaultLogAdapter popFaultLogAdapter;
    private PopCarePlanAdapter popCarePlanAdapter;
    private PopMonthAdapter popMonthAdapter;

    private int stationId;
    private String date;
    private String selectEleNo;
    private String selectMonth;

    public MonthFormDialog(Context context, int themeResId, int stationId, String date) {
        super(context, themeResId);
        mRxBus= SmecRxBus.get();
        mRxBus.register(this);
        mContext = context;
        monthFormPresenter = new MonthFormPresenter(mContext);
        dm = mContext.getResources().getDisplayMetrics();
        popWindowAdapter = new PopWindowAdapter(dm,mRxBus,monthFormPresenter,this);
        popFaultLogAdapter = new PopFaultLogAdapter();
        popCarePlanAdapter = new PopCarePlanAdapter();

        this.stationId = stationId;
        this.date = date;
        ArrayList<String> month = new ArrayList<>();
        month.add("一个月");
        month.add("三个月");
        month.add("半年");
        popMonthAdapter = new PopMonthAdapter(month,mRxBus, SmecRailApi.MonthCode.MONTHFORM);
        selectMonth = "一个月";
        dialogMonthFormBinding = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.dialog_month_form,null,false);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(dialogMonthFormBinding.getRoot());
        _initView();
    }

    private void _initView(){
        /*******Adapter设置***********/
        //弹出框Adapter设置
        dialogMonthFormBinding.popWindowContent.setLayoutManager(new LinearLayoutManager(mContext));
        dialogMonthFormBinding.popWindowContent.setAdapter(popWindowAdapter);
        //故障日志
        dialogMonthFormBinding.faultLogList.setLayoutManager(new LinearLayoutManager(mContext));
        dialogMonthFormBinding.faultLogList.setAdapter(popFaultLogAdapter);
        //保养计划
        dialogMonthFormBinding.carePlanList.setLayoutManager(new LinearLayoutManager(mContext));
        dialogMonthFormBinding.carePlanList.setAdapter(popCarePlanAdapter);
        //弹出框月份选择
        dialogMonthFormBinding.monthList.setLayoutManager(new LinearLayoutManager(mContext));
        dialogMonthFormBinding.monthList.setAdapter(popMonthAdapter);

        CommonUtils.setCameraDistance(mContext.getResources().getDisplayMetrics(),dialogMonthFormBinding.card);
        
        /*******点击事件设置***********/
        dialogMonthFormBinding.popWindowDetailBack.setOnClickListener(v -> {
            CommonUtils.cardFront(dialogMonthFormBinding.card, R.id.popWindow, R.id.popWindowDetail);

            dialogMonthFormBinding.getRoot().postDelayed(new Runnable() {
                @Override
                public void run() {
                    dialogMonthFormBinding.refreshList.startRefresh();

                    popFaultLogAdapter.start =0 ;
                    popFaultLogAdapter.setList(new ArrayList<>());
                    popFaultLogAdapter.notifyDataSetChanged();

                    popCarePlanAdapter.start =0 ;
                    popCarePlanAdapter.setList(new ArrayList<>());
                    popCarePlanAdapter.notifyDataSetChanged();
                }
            },800);

        });

        //设置弹出框详情按钮的点击事件
        dialogMonthFormBinding.carePlanBtn.setOnClickListener(v -> {
            if(dialogMonthFormBinding.carePlan.getVisibility() != View.VISIBLE){
                dialogMonthFormBinding.carePlan.setVisibility(View.VISIBLE);
                dialogMonthFormBinding.faultLog.setVisibility(View.INVISIBLE);
                dialogMonthFormBinding.carePlanBtn.setTextColor(mContext.getResources().getColor(R.color.customWhite));
                dialogMonthFormBinding.carePlanBtn.setBackground(null);
                dialogMonthFormBinding.faultLogBtn.setTextColor(mContext.getResources().getColor(R.color.monthForm_title));
                dialogMonthFormBinding.faultLogBtn.setBackgroundColor(mContext.getResources().getColor(R.color.smec_gray_bottom));
            }
        });
        dialogMonthFormBinding.faultLogBtn.setOnClickListener(v -> {
            if(dialogMonthFormBinding.faultLog.getVisibility() != View.VISIBLE){
                dialogMonthFormBinding.carePlan.setVisibility(View.INVISIBLE);
                dialogMonthFormBinding.faultLog.setVisibility(View.VISIBLE);
                dialogMonthFormBinding.carePlanBtn.setTextColor(mContext.getResources().getColor(R.color.monthForm_title));
                dialogMonthFormBinding.carePlanBtn.setBackgroundColor(mContext.getResources().getColor(R.color.smec_gray_bottom));
                dialogMonthFormBinding.faultLogBtn.setTextColor(mContext.getResources().getColor(R.color.customWhite));
                dialogMonthFormBinding.faultLogBtn.setBackground(null);
            }
        });

        //弹出框-故障日志-月份点击事件
        dialogMonthFormBinding.changePopMonth.setOnClickListener(v -> {
            if(dialogMonthFormBinding.popMonthBg.getVisibility() == View.VISIBLE){
                dialogMonthFormBinding.popMonthBg.setVisibility(View.INVISIBLE);
                dialogMonthFormBinding.popMonthImg.setBackgroundResource(R.drawable.month_form_bottom);

            }else{
                dialogMonthFormBinding.popMonthBg.setVisibility(View.VISIBLE);
                dialogMonthFormBinding.popMonthImg.setBackgroundResource(R.drawable.month_form_top);
            }
        });
        dialogMonthFormBinding.popMonthBg.setOnClickListener(v -> {
            v.setVisibility(View.INVISIBLE);
            dialogMonthFormBinding.popMonthImg.setBackgroundResource(R.drawable.month_form_bottom);
        });

        /*********界面设置***********/
        dialogMonthFormBinding.popMonthImg.setBackgroundResource(R.drawable.month_form_bottom);
        
        /********下拉刷新设置********/
        //站点故障次数下拉刷新
        dialogMonthFormBinding.refreshList.setHeaderView(SmecRailConstant.getSinaRefreshView());
        dialogMonthFormBinding.refreshList.setAutoLoadMore(true);
        dialogMonthFormBinding.refreshList.setEnableOverScroll(false);
        dialogMonthFormBinding.refreshList.setEnableLoadmore(false);
        dialogMonthFormBinding.refreshList.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                getStationForm(date,stationId);
            }
        });
        //故障日志下拉刷新
        dialogMonthFormBinding.refreshFaultLog.setHeaderView(SmecRailConstant.getSinaRefreshView());
        dialogMonthFormBinding.refreshFaultLog.setAutoLoadMore(true);
        dialogMonthFormBinding.refreshFaultLog.setEnableOverScroll(false);
        dialogMonthFormBinding.refreshFaultLog.setEnableLoadmore(false);
        dialogMonthFormBinding.refreshFaultLog.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                getFaultLog(selectEleNo);
            }
        });
        //保养计划下拉刷新
        dialogMonthFormBinding.refreshCarePlan.setHeaderView(SmecRailConstant.getSinaRefreshView());
        dialogMonthFormBinding.refreshCarePlan.setAutoLoadMore(true);
        dialogMonthFormBinding.refreshCarePlan.setEnableOverScroll(false);
        dialogMonthFormBinding.refreshCarePlan.setEnableLoadmore(false);
        dialogMonthFormBinding.refreshCarePlan.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                getCarePlan(selectEleNo);
            }
        });

        dialogMonthFormBinding.refreshList.startRefresh();

    }

    /**
     * 获取站点故障报表成功
     */
    @Subscribe(

            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(GET_STATION_FORM_SUCCESS)
            }
    )
    public void getStationFormSuccess(ArrayList<PositionBean> list){
        popWindowAdapter.showAnimation = true;
        popWindowAdapter.setList(list);
        popWindowAdapter.notifyDataSetChanged();
        dialogMonthFormBinding.getRoot().postDelayed(new Runnable() {
            @Override
            public void run() {
                popWindowAdapter.showAnimation = false;
            }
        },500);
        dialogMonthFormBinding.refreshList.finishRefreshing();
    }

    /**
     * 获取站点故障报表失败
     */
    @Subscribe(

            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(GET_STATION_FORM_FAILED)
            }
    )
    public void getStationFormFailed(Object o){
        popWindowAdapter.showAnimation = true;
        popWindowAdapter.setList(new ArrayList<>());
        popWindowAdapter.notifyDataSetChanged();
        dialogMonthFormBinding.getRoot().postDelayed(new Runnable() {
            @Override
            public void run() {
                popWindowAdapter.showAnimation = false;
            }
        },500);
        dialogMonthFormBinding.refreshList.finishRefreshing();
    }

    /**
     * 展示电梯故障详情
     */
    @Subscribe(

            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(SHOW_POP_DETAIL)
            }
    )
    public void showPopDetail(PositionBean o){
        selectEleNo = o.getEleContractNo();
        dialogMonthFormBinding.popWindowDetailTitle.setText(o.getEleCode());
        CommonUtils.cardBack(dialogMonthFormBinding.card, dialogMonthFormBinding.popWindowDetail, R.id.popWindow, R.id.popWindowDetail);
        dialogMonthFormBinding.getRoot().postDelayed(new Runnable() {
            @Override
            public void run() {
                dialogMonthFormBinding.refreshCarePlan.startRefresh();
                dialogMonthFormBinding.refreshFaultLog.startRefresh();

                popWindowAdapter.setList(new ArrayList<>());
                popWindowAdapter.notifyDataSetChanged();
            }
        },800);


    }

    /**
     * 获取故障日志成功
     */
    @Subscribe(

            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(GET_FAULT_LOG_SUCCESS)
            }
    )
    public void getFaultLogSuccess(FaultLogBean faultLogBean){
        dialogMonthFormBinding.faultLogNum.setText("共计："+ faultLogBean.getTotal() +" 条");
        popFaultLogAdapter.setList(faultLogBean.getFault());
        dialogMonthFormBinding.getRoot().postDelayed(new Runnable() {
            @Override
            public void run() {
                dialogMonthFormBinding.refreshFaultLog.finishRefreshing();
            }
        },500);
    }

    /**
     * 获取故障日志失败
     */
    @Subscribe(

            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(GET_FAULT_LOG_FAILED)
            }
    )
    public void getFaultLogFailed(Object o){
        dialogMonthFormBinding.faultLogNum.setText("共计：0 条");
        popFaultLogAdapter.setList(new ArrayList<>());
        dialogMonthFormBinding.getRoot().postDelayed(new Runnable() {
            @Override
            public void run() {
                dialogMonthFormBinding.refreshFaultLog.finishRefreshing();
            }
        },500);
    }

    /**
     * 获取保养计划成功
     */
    @Subscribe(

            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(GET_CARE_PLAN_SUCCESS)
            }
    )
    public void getCarePlanSuccess(ArrayList<CarePlanBean> list){
        popCarePlanAdapter.setList(list);
        dialogMonthFormBinding.getRoot().postDelayed(new Runnable() {
            @Override
            public void run() {
                dialogMonthFormBinding.refreshCarePlan.finishRefreshing();
            }
        },500);
    }

    /**
     * 获取保养计划失败
     */
    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(GET_CARE_PLAN_FAILED)
            }
    )
    public void getCarePlanFailed(Object o){
        popCarePlanAdapter.setList(new ArrayList<CarePlanBean>());
        dialogMonthFormBinding.getRoot().postDelayed(new Runnable() {
            @Override
            public void run() {
                dialogMonthFormBinding.refreshCarePlan.finishRefreshing();
            }
        },500);
    }

    /**
     * 弹出框月份选择事件
     */
    @Subscribe(

            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(CHANGE_POP_MONTH)
            }
    )
    public void changePopMonth(Object o){
        dialogMonthFormBinding.popMonth.setText((String)o);
        selectMonth = (String)o;
        dialogMonthFormBinding.popMonthBg.setVisibility(View.INVISIBLE);
        dialogMonthFormBinding.popMonthImg.setBackgroundResource(R.drawable.month_form_bottom);
        getFaultLog(selectEleNo);
    }

    //获取站点的故障报表
    public void getStationForm(String date,int stationId){
        //设置参数
        HashMap<String, Object> param = new HashMap<>();
        param.put("date",date);
        param.put("stationId",stationId);

        //网络请求
        this.monthFormPresenter.getStationForm(param);
    }

    //获取某一电梯的故障日志
    public void getFaultLog(String eleContractNo){
        popFaultLogAdapter.start = 1;
        //设置参数
        HashMap<String, Object> param = new HashMap<>();
        switch (selectMonth){
            case "一个月": param.put("month",1);break;
            case "三个月": param.put("month",3);break;
            case "半年": param.put("month",6);break;
            default:param.put("month",1);
        }
        param.put("eleContractNo",eleContractNo);

        //网络请求
        this.monthFormPresenter.getFaultLog(param);
    }

    //获取某一电梯的保养计划
    public void getCarePlan(String eleContractNo){
        popCarePlanAdapter.start = 1;
        //设置参数
        HashMap<String, Object> param = new HashMap<>();
        param.put("eleContractNo",eleContractNo);

        //网络请求
        this.monthFormPresenter.getCarePlan(param);
    }

    public void setTitle(String title){
        dialogMonthFormBinding.popWindowTitle.setText(title);
    }
}
